// pages/play/play.js
import moment from 'moment'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    musicid: '',//当前音乐的id
    musiclist: [],//歌曲列表
    nowIndex: '', //正在播放音乐的索引
    music: {},//正在播放的音乐
    lyriclist: [],//歌词
    currentTime: '0:00',//实时时间
    durationTiom: '00:00',//总时长
    isPlay: true,//控制播放与暂停
    currentWidth: 0,//实时进度条长度
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', (data) => {
      const { index, newMusicList } = data.data
      const musicid = newMusicList[index].id
      //获取当前播放的歌曲
      this.setData({
        nowIndex: index,
        musiclist: newMusicList,
        musicid
      })
      //获取歌曲详情
      this.getMusicDetail()
      //获取歌词
      this.getLyric()
      //播放歌曲
      this.musicControl(this.data.isPlay, musicid)
      //点击自带暂停按钮
      this.backgroundAudioManager = wx.getBackgroundAudioManager();
      this.backgroundAudioManager.onPlay(() => {
        this.setData({
          isPlay: true
        })
      })
      this.backgroundAudioManager.onPause(() => {
        this.setData({
          isPlay: false
        })
      })
      //监听音乐实时播放
      this.backgroundAudioManager.onTimeUpdate(() => {
        let currentWidth = this.backgroundAudioManager.currentTime / this.backgroundAudioManager.duration * 100
        const currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss')
        this.setData({
          currentTime,
          currentWidth
        })
      })
    })
  },
  //根据id获取歌单详情
  getMusicDetail() {
    wx.request({
      url: 'http://localhost:3000/song/detail?ids=' + this.data.musicid,
      success: (res) => {
        const music = res.data.songs[0];
        let durationTiom = moment(music.dt).format('mm:ss')
        this.setData({
          music, durationTiom
        })
      },
    })
  },
  //播放控制
  playdate() {
    const isPlay = !this.data.isPlay
    this.musicControl(isPlay, this.data.musicid)
  },
  //获取歌词
  getLyric() {
    wx.request({
      url: 'http://localhost:3000/lyric?id=' + this.data.musicid,
      success: (res) => {
        this.setlrcshow(res.data.lrc.lyric)
      },
    })
  },
  //整理歌词
  setlrcshow(lrc) {
    //定义时间歌词
    let list = []
    const lrclist = lrc.split('\n')
    lrclist.pop()
    const re = /\[\d{2}:\d{2}\.\d{2,3}\]/
    lrclist.forEach(item => {
      if (item) {
        let itemdate = item.match(re)[0]
        itemdate = itemdate.slice(1, -1)
        //装换为秒
        const time1 = itemdate.split(':')[0]
        const time2 = itemdate.split(':')[1]
        const time = time1 * 60 + time2 * 1
        // 找歌词
        let lrc = item.replace(re, '')
        //整理时间，歌词
        list.push([time, lrc])
      }
    });
    this.setData({
      lyriclist: list
    })
  },
  //控制音乐的播放/暂停功能
  musicControl(isPlay, musicid) {

    if (isPlay) {
      wx.request({
        url: 'http://localhost:3000/song/url?id=' + musicid,
        success: (res) => {
          let musicLink = res.data.data[0].url
          this.backgroundAudioManager.src = musicLink
          this.backgroundAudioManager.title = this.data.music.name
        },
      })
    }
    else {
      this.backgroundAudioManager.pause()
    }
  },
  //切换音乐
  changeMusic(e) {

    const num = e.currentTarget.dataset.index;
    let nowIndex
    if (num == -1) {
      if (this.data.nowIndex == 0) {
        nowIndex = this.data.musiclist.length - 1
        this.setData({
          nowIndex,
          musicid: this.data.musiclist[nowIndex].id
        })
      }
      else {
        nowIndex = this.data.nowIndex - 1
        this.setData({
          nowIndex,
          musicid: this.data.musiclist[nowIndex].id
        })
      }
    }
    else {
      if (this.data.nowIndex == this.data.musiclist.length - 1) {
        nowIndex = 0
        this.setData({
          nowIndex,
          musicid: this.data.musiclist[nowIndex].id
        })
      }
      else {
        nowIndex = this.data.nowIndex + 1
      }
    }
    //进行数据的更改
    this.setData({
      nowIndex,
      musicid: this.data.musiclist[nowIndex].id
    })
    //获取歌曲详情
    this.getMusicDetail()
    //获取歌词
    this.getLyric()
    //播放歌曲
    this.musicControl(this.data.isPlay, this.data.musicid)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})